package com.xxx.dao;

import com.xxx.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.Data;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xxx
 * @since 2023-10-30
 */
public interface RoleMapper extends BaseMapper<Role> {
    /**
     * 根据角色id获取用户数量
     * @param id
     * @return
     */
    @Select("SELECT count(1) FROM sys_user_role WHERE role_id = #{id}")
    int  getRoleCountByRoleId(Long id);

    /**
     * 删除角色权限关系
     */
    @Delete("DELETE FROM sys_role_permission WHERE role_id = #{id}")
    void deleteRolePermissionByRoleId(Long id);

    /**
     * 保存角色权限关系
     * @param roleId
     * @param permissionIds
     * @return
     */
    int saveRolePermission(Long roleId, List<Long> permissionIds);

    /**
     * 根据用户ID查询该用户拥有的角色ID
     * @param userId
     * @return
     */
    @Select("select role_id from sys_user_role where user_id = #{userId}")
    List<Long> findRoleIdByUserId(Long userId);
    /**
     * 根据用户ID查询该用户拥有的角色名称
     * @param userId
     * @return
     */
    @Select("select role_name from sys_role where id in (select role_id from sys_user_role where user_id = #{userId}) order by id")
    List<String> findRoleNameByUserId(Long userId);
}
